<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/12
 * Time: 16:00
 */
namespace Mhome\Controller;

use Common\Model\CommonModel;

class TeamExamineController extends MhomeController {

    /**
     * 获取班子考评列表
     */
    public function getUserTeamExamineList(){
        $team_examine_relation_array = array();
        $user_type_str = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $user_type_str = "down";
            $team_examine_relation_array = M("TeamExamineRelationDown")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("team_examine_id,be_examine_team_id,comprehensive_status,employing_status,new_p_status");
        }

        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $user_type_str = "same";
            $team_examine_relation_array = M("TeamExamineRelationSame")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("team_examine_id,be_examine_team_id,comprehensive_status,employing_status,new_p_status");
        }

        if($this->user_data['rank'] == 5) {
            $user_type_str = "up";
            $team_examine_relation_array = M("TeamExamineRelationUp")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("team_examine_id,be_examine_team_id,comprehensive_status,employing_status,new_p_status");
        }

        if(!$team_examine_relation_array){
            return [];
        }

        $team_examine_id_array = array_keys($team_examine_relation_array);

        $field = "team_examine_id,status,title,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,add_time";

        $where = array();
        $where['team_examine_id'] = ['in',$team_examine_id_array];
        $where['status'] = ['in','2,3'];            //状态：0未设置，1未开始，2进行中，3已结束

        $examine_list_data = M("TeamExamine")
            ->where($where)
            ->field($field)
            ->order("status,add_time desc")
            ->select();

        if(!$examine_list_data){
            return [];
        }

        $return_examine_list = array();

        $current_time = time();
        $status_str_array = ["0"=>"未开始","1"=>"进行中","2"=>"已结束","3"=>"已考核"];
        foreach($examine_list_data as $key=>$value){
            /**
             * "examine_id": "26",
            "examine_type": "1",       //考核类型，1中层干部考评，2班子综合考评，3班子选人用人情况考评，4新提拔人员考评
            "status": 3,            //考核状态，0未开始，1进行中，2已结束，3已考核
            "title": "考核1",
            "add_time": "2019-12-02 22:33:33",
            "examine_time": "2019-12-02---2019-12-04",
            "status_str": "已考核"
             */
            $add_time_str = changeDateTime($value['add_time']);
            //预设综合考评数据
            $comprehensive_examine_data = array();
            $comprehensive_examine_data['examine_id'] = $value['team_examine_id'];
            $comprehensive_examine_data['examine_type'] = 2;
            $comprehensive_examine_data['status'] = 0;
            $comprehensive_examine_data['status_str'] = $status_str_array[0];
            $comprehensive_examine_data['title'] = $value['title']."——"."综合考评";
            $comprehensive_examine_data['add_time'] = $add_time_str;
            $comprehensive_examine_data['examine_time'] = changeDate($value[$user_type_str.'_start_time'])."——".
                changeDate($value[$user_type_str.'_end_time']);
            //预设选人用人情况考评
            $employing_examine_data = array();
            $employing_examine_data['examine_id'] = $value['team_examine_id'];
            $employing_examine_data['examine_type'] = 3;
            $employing_examine_data['status'] = 0;
            $employing_examine_data['status_str'] = $status_str_array[0];
            $employing_examine_data['title'] = $value['title']."——"."选人用人情况考评";
            $employing_examine_data['add_time'] = $add_time_str;
            $employing_examine_data['examine_time'] = changeDate($value[$user_type_str.'_start_time'])."——".
                changeDate($value[$user_type_str.'_end_time']);
            //预设新提拔人员考评
            $new_p_examine_data = array();
            $new_p_examine_data['examine_id'] = $value['team_examine_id'];
            $new_p_examine_data['examine_type'] = 4;
            $new_p_examine_data['status'] = 0;
            $new_p_examine_data['status_str'] = $status_str_array[0];
            $new_p_examine_data['title'] = $value['title']."——"."新提拔人员考评";
            $new_p_examine_data['add_time'] = $add_time_str;
            $new_p_examine_data['examine_time'] = changeDate($value[$user_type_str.'_start_time'])."——".
                changeDate($value[$user_type_str.'_end_time']);

            //判断审核状态
            if($value['status'] == 3){
                //评审已结束
                $comprehensive_examine_data['status'] = 2;
                $comprehensive_examine_data['status_str'] = $status_str_array[2];
                $employing_examine_data['status'] = 2;
                $employing_examine_data['status_str'] = $status_str_array[2];
                $new_p_examine_data['status'] = 2;
                $new_p_examine_data['status_str'] = $status_str_array[2];
            }else{
                //评审进行中
                //判断是否已经审评,comprehensive_status,employing_status,new_p_status
                //综合考评
                if($team_examine_relation_array[$value['team_examine_id']]['comprehensive_status'] == 1){
                    $comprehensive_examine_data['status'] = 3;
                    $comprehensive_examine_data['status_str'] = $status_str_array[3];
                }else{
                    //判断是否到已经审评的时间
                    //判断是否已经开始评审
                    if($value[$user_type_str.'_start_time'] < $current_time){
                        $comprehensive_examine_data['status'] = 1;
                        $comprehensive_examine_data['status_str'] = $status_str_array[1];
                    }
                    //判断是否已经结束评审
                    if($value[$user_type_str.'_end_time'] < $current_time){
                        $comprehensive_examine_data['status'] = 2;
                        $comprehensive_examine_data['status_str'] = $status_str_array[1];
                    }
                }

                //选人用人情况考评
                if($team_examine_relation_array[$value['team_examine_id']]['employing_status'] == 1){
                    $employing_examine_data['status'] = 3;
                    $employing_examine_data['status_str'] = $status_str_array[3];
                }else{
                    //判断是否到已经审评的时间
                    //判断是否已经开始评审
                    if($value[$user_type_str.'_start_time'] < $current_time){
                        $employing_examine_data['status'] = 1;
                        $employing_examine_data['status_str'] = $status_str_array[1];
                    }
                    //判断是否已经结束评审
                    if($value[$user_type_str.'_end_time'] < $current_time){
                        $employing_examine_data['status'] = 2;
                        $employing_examine_data['status_str'] = $status_str_array[1];
                    }
                }

                //新提拔人员考评
                if($team_examine_relation_array[$value['team_examine_id']]['new_p_status'] == 1){
                    $new_p_examine_data['status'] = 3;
                    $new_p_examine_data['status_str'] = $status_str_array[3];
                }else{
                    //判断是否到已经审评的时间
                    //判断是否已经开始评审
                    if($value[$user_type_str.'_start_time'] < $current_time){
                        $new_p_examine_data['status'] = 1;
                        $new_p_examine_data['status_str'] = $status_str_array[1];
                    }
                    //判断是否已经结束评审
                    if($value[$user_type_str.'_end_time'] < $current_time){
                        $new_p_examine_data['status'] = 2;
                        $new_p_examine_data['status_str'] = $status_str_array[1];
                    }
                }
            }
            $return_examine_list[] = $comprehensive_examine_data;
            if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
                $return_examine_list[] = $employing_examine_data;
                $check_new_p_count = M("TeamExamineNewPromotion")
                    ->where(['team_examine_id'=>$value['team_examine_id'],'team_id'=>$team_examine_relation_array[$value['team_examine_id']]['be_examine_team_id']])
                    ->count();
                if($check_new_p_count){
                    $return_examine_list[] = $new_p_examine_data;
                }
            }
        }
        return $return_examine_list;
    }

    /**
     * 综合考评
     * 获取详情接口
     */
    public function getComprehensiveExamineInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,item_json")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }

        $current_time = time();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }
            $user_type = "Up";
        }

        $examine_relation = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id",true);
        if(!$examine_relation){
            $this->returnJson(503,"无数据");
        }

        $team_list = M("Team")
            ->where(['team_id'=>['in',$examine_relation]])
            ->field("team_id,name,name_abb")
            ->select();

        if(!$team_list){
            $this->returnJson(503,"无数据");
        }

        $return_data = array();
        $return_data['item_array'] = json_decode($team_data['item_json'],true);
        $return_data['team_list'] = $team_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 综合考评
     * 获取已考评详情接口
     */
    public function getComprehensiveExamineResultInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,item_json")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        /*if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }*/

        $current_time = time();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            /*if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }*/
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            /*if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }*/
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            /*if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }*/
            $user_type = "Up";
        }

        $examine_relation_data = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id,item_result,item_score");
        if(!$examine_relation_data){
            $this->returnJson(503,"无数据");
        }
        $examine_relation_id_array = array_keys($examine_relation_data);

        $team_list = M("Team")
            ->where(['team_id'=>['in',$examine_relation_id_array]])
            ->field("team_id,name,name_abb")
            ->select();

        if(!$team_list){
            $this->returnJson(503,"无数据");
        }
        foreach($team_list as $key=>$value){
            $team_list[$key]['item_result'] = json_decode($examine_relation_data[$value['team_id']]['item_result'],true);
            $team_list[$key]['item_score'] = floatval($examine_relation_data[$value['team_id']]['item_score']);
        }

        $return_data = array();
        $return_data['item_array'] = json_decode($team_data['item_json'],true);
        $return_data['team_list'] = $team_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 综合考评
     * 提交数据接口
     */
    public function submitComprehensiveExamineData(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        //addFileLog("submitComprehensiveExamineData".":REQUEST:".json_encode($_REQUEST));

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }

        $examine_json = $_REQUEST['examine_json']?$_REQUEST['examine_json']:$this->errorReturn("请输入考核数据JSON");
        $examine_array = json_decode($examine_json,true);
        if(!is_array($examine_array)){
            $this->errorReturn("考核数据格式错误");
        }
        if(!$examine_array){
            $this->errorReturn("请输入考核数据");
        }
        //检查班子ID是否正确
        $team_id_array = array_column($examine_array,0);
        $team_id_array = array_keys(array_flip($team_id_array));
        $check_team_id = M("Team")
            ->where(['team_id'=>['in',$team_id_array]])
            ->count();

        if($check_team_id != count($team_id_array)){
            $this->errorReturn("存在错误的班子ID参数");
        }
        //检查考评数据量是否正确
        $examine_item_list = M("TeamExamineItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->getField("title",true);
        if(!$examine_item_list){
            $this->errorReturn("班子考评ID错误");
        }
        $item_count = count($examine_item_list);
        foreach($examine_array as $key=>$value){
            if($item_count != count($value) - 1){
                $this->errorReturn("考核数据JSON，格式错误,指标数据量不符");
            }
        }
        $model = M();
        $model->startTrans();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $user_type = "Same";
        }elseif ($this->user_data['rank'] == 5){
            $user_type = "Up";
        }

        foreach($examine_array as $key=>$value){
            $examine_result = $value;
            unset($examine_result[0]);
            $examine_result = json_encode(array_values($examine_result));
            //计算总得分
            $score = 0;
            for($i=1;$i<count($value);$i++){
                if($value[$i] < 0 || $value[$i] > 10){
                    $model->rollback();
                    $this->errorReturn("一个指标分数必须是0到10分");
                }
                $score = bcadd($score,$value[$i],4);
            }
            //comprehensive_status,item_result,item_score
            $new_data = array();
            $new_data['comprehensive_status'] = 1;
            $new_data['item_result'] = $examine_result;
            $new_data['item_score'] = $score;

            $update_result = M("TeamExamineRelation".$user_type)
                ->where(['team_examine_id'=>$team_examine_id,'be_examine_team_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                ->save($new_data);

            if(!$update_result){
                $model->rollback();
                $this->errorReturn("提交数据失败，请重新尝试");
            }
        }

        $model->commit();
        $this->successReturn("提交成功");
    }

    /**
     * 选人用人考评
     * 获取详情接口
     */
    public function getEmployingExamineInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,employing_item_json")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }

        $current_time = time();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }
            $user_type = "Up";
        }

        $examine_relation = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id",true);
        if(!$examine_relation){
            $this->returnJson(503,"无数据");
        }

        $team_list = M("Team")
            ->where(['team_id'=>['in',$examine_relation]])
            ->field("team_id,name,name_abb")
            ->select();

        if(!$team_list){
            $this->returnJson(503,"无数据");
        }

        $return_data = array();
        $return_data['item_array'] = json_decode($team_data['employing_item_json'],true);
        $return_data['team_list'] = $team_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 选人用人考评
     * 获取已考评详情接口
     */
    public function getEmployingExamineResultInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,employing_item_json")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        /*if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }*/

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            /*if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }*/
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            /*if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }*/
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            /*if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }*/
            $user_type = "Up";
        }

        $examine_relation_data = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id,employing_item_result");
        if(!$examine_relation_data){
            $this->returnJson(503,"无数据");
        }

        $examine_relation_id_array = array_keys($examine_relation_data);

        $team_list = M("Team")
            ->where(['team_id'=>['in',$examine_relation_id_array]])
            ->field("team_id,name,name_abb")
            ->select();

        if(!$team_list){
            $this->returnJson(503,"无数据");
        }

        foreach($team_list as $key=>$value){
            $team_list[$key]['employing_item_result'] = json_decode($examine_relation_data[$value['team_id']],true);
        }

        $return_data = array();
        $return_data['item_array'] = json_decode($team_data['employing_item_json'],true);
        $return_data['team_list'] = $team_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 选人用人考评
     * 提交数据接口
     */
    public function submitEmployingExamineData(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }

        $examine_json = $_REQUEST['examine_json']?$_REQUEST['examine_json']:$this->errorReturn("请输入考核数据JSON");
        $examine_array = json_decode($examine_json,true);
        if(!is_array($examine_array)){
            $this->errorReturn("考核数据格式错误");
        }
        if(!$examine_array){
            $this->errorReturn("请输入考核数据");
        }
        //检查班子ID是否正确
        $team_id_array = array_column($examine_array,0);
        $team_id_array = array_keys(array_flip($team_id_array));
        $check_team_id = M("Team")
            ->where(['team_id'=>['in',$team_id_array]])
            ->count();

        if($check_team_id != count($team_id_array)){
            $this->errorReturn("存在错误的班子ID参数");
        }
        //检查考评数据量是否正确
        $examine_item_list = M("TeamExamineEmployingItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->getField("title",true);
        if(!$examine_item_list){
            $this->errorReturn("班子考评ID错误");
        }
        $item_count = count($examine_item_list);
        foreach($examine_array as $key=>$value){
            if($item_count != count($value) - 1){
                $this->errorReturn("考核数据JSON，格式错误,指标数据量不符");
            }
        }
        $model = M();
        $model->startTrans();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $user_type = "Same";
        }elseif ($this->user_data['rank'] == 5){
            $user_type = "Up";
        }

        foreach($examine_array as $key=>$value){
            $examine_result = $value;
            unset($examine_result[0]);
            $examine_result = json_encode(array_values($examine_result));

            for($i=1;$i<count($value);$i++){
                if($value[$i] < 0 || $value[$i] > 4){
                    $model->rollback();
                    $this->errorReturn("指标参数只能是1~4，分别是：1满意，2基本满意，3不满意，4，不了解");
                }
            }

            $new_data = array();
            $new_data['employing_status'] = 1;
            $new_data['employing_item_result'] = $examine_result;

            $update_result = M("TeamExamineRelation".$user_type)
                ->where(['team_examine_id'=>$team_examine_id,'be_examine_team_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                ->save($new_data);

            if(!$update_result){
                $model->rollback();
                $this->errorReturn("提交数据失败，请重新尝试");
            }
        }

        $model->commit();
        $this->successReturn("提交成功");
    }

    /**
     * 新提拔人员考评
     * 获取详情接口
     */
    public function getNewPExamineInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }

        $current_time = time();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }
            $user_type = "Up";
        }

        $examine_relation = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id",true);
        if(!$examine_relation){
            $this->returnJson(503,"无数据");
        }

        $new_p_list = M("TeamExamineNewPromotion")
            ->where(['team_examine_id'=>$team_examine_id,'team_id'=>['in',$examine_relation]])
            ->field("new_p_num,team_id,name,date_birth,former_duties,now_duties,appointment_time")
            ->select();

        if(!$new_p_list){
            $this->returnJson(503,"无数据");
        }

        $team_id_array = array_column($new_p_list,"team_id");
        $team_name_array = M("Team")
            ->where(['team_id'=>['in',$team_id_array]])
            ->getField("team_id,name");
        if(!$team_name_array){
            $team_name_array = array();
        }

        foreach($new_p_list as $key=>$value){
            $new_p_list[$key]['team_name'] = $team_name_array[$value['team_id']];

            $new_p_list[$key]['date_birth'] = date("Y-m",$value['date_birth']);
            $new_p_list[$key]['appointment_time'] = changeDate($value['appointment_time']);
        }

        $return_data = array();
        $return_data['new_p_list'] = $new_p_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 新提拔人员考评
     * 获取已考评详情接口
     */
    public function getNewPExamineResultInfo(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time")
            ->find();
        if(!$team_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }
        /*if($team_data['status'] != 2){
            $this->errorReturn("考核未开始或已结束，无法获取详情");
        }*/

        $current_time = time();

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
            /*if($team_data['down_start_time'] > $current_time || $team_data['down_end_time'] < $current_time){
                $this->errorReturn("员工考核时间为：".changeDate($team_data['down_start_time'])."到".changeDate($team_data['down_end_time']));
            }*/
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            /*if($team_data['same_start_time'] > $current_time || $team_data['same_end_time'] < $current_time){
                $this->errorReturn("中层考核时间为：".changeDate($team_data['same_start_time'])."到".changeDate($team_data['same_end_time']));
            }*/
            $user_type = "Same";
        }elseif($this->user_data['rank'] == 5){
            /*if($team_data['up_start_time'] > $current_time || $team_data['up_end_time'] < $current_time){
                $this->errorReturn("高层考核时间为：".changeDate($team_data['up_start_time'])."到".changeDate($team_data['up_end_time']));
            }*/
            $user_type = "Up";
        }

        $examine_relation = M("TeamExamineRelation".$user_type)
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
            ->getField("be_examine_team_id,new_p_result");
        if(!$examine_relation){
            $this->returnJson(503,"无数据");
        }
        $examine_relation_id_array = array_keys($examine_relation);

        $n_p_num_array = array();
        foreach($examine_relation as $key=>$value){
            $new_p_result = json_decode($value,true);
            if($new_p_result){
                foreach($new_p_result as $key2=>$value2){
                    $n_p_num_array[$key2] = $value2;
                }
            }
        }

        $new_p_list = M("TeamExamineNewPromotion")
            ->where(['team_examine_id'=>$team_examine_id,'team_id'=>['in',$examine_relation_id_array]])
            ->field("new_p_num,team_id,name,date_birth,former_duties,now_duties,appointment_time")
            ->select();

        if(!$new_p_list){
            $this->returnJson(503,"无数据");
        }

        $team_id_array = array_column($new_p_list,"team_id");
        $team_name_array = M("Team")
            ->where(['team_id'=>['in',$team_id_array]])
            ->getField("team_id,name");
        if(!$team_name_array){
            $team_name_array = array();
        }

        foreach($new_p_list as $key=>$value){
            $new_p_list[$key]['team_name'] = $team_name_array[$value['team_id']];

            $new_p_list[$key]['date_birth'] = date("Y-m",$value['date_birth']);
            $new_p_list[$key]['appointment_time'] = changeDate($value['appointment_time']);

            $new_p_list[$key]['new_p_result'] = isset($n_p_num_array[$value['new_p_num']])?$n_p_num_array[$value['new_p_num']]:"";
        }

        $return_data = array();
        $return_data['new_p_list'] = $new_p_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 新提拔人员考评
     * 提交数据接口
     */
    public function submitNewPExamineData(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }

        $examine_json = $_REQUEST['examine_json']?$_REQUEST['examine_json']:$this->errorReturn("请输入考核数据JSON");
        $examine_array = json_decode($examine_json,true);
        if(!is_array($examine_array)){
            $this->errorReturn("考核数据格式错误");
        }
        if(!$examine_array){
            $this->errorReturn("请输入考核数据");
        }
        //检查班子ID是否正确
        $new_p_num_array = array_column($examine_array,0);
        $new_p_num_array = array_keys(array_flip($new_p_num_array));
        $check_new_p_num = M("TeamExamineNewPromotion")
            ->where(['team_examine_id'=>$team_examine_id,'new_p_num'=>['in',$new_p_num_array]])
            ->getField("new_p_num,team_id");

        if(count($check_new_p_num) != count($new_p_num_array)){
            $this->errorReturn("存在错误的新提拔人员序号参数");
        }
        //检查考评数据量是否正确
        foreach($examine_array as $key=>$value) {
            if (1 != count($value) - 1) {
                $this->errorReturn("考核数据JSON，格式错误,数据量不符");
            }
        }

        $user_type = "";
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $user_type = "Down";
        }elseif ($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $user_type = "Same";
        }elseif ($this->user_data['rank'] == 5){
            $user_type = "Up";
        }

        $new_examine_result = array();
        foreach($examine_array as $key=>$value){
            $new_examine_result[$check_new_p_num[$value[0]]][$value[0]] = $value[1];
        }

        if(!$new_examine_result){
            $this->errorReturn("无数据");
        }

        $model = M();
        $model->startTrans();
        foreach($new_examine_result as $key=>$value){
            $examine_result = $value;
            $examine_result = json_encode($examine_result);

            foreach($value as $key2=>$value2){
                if($value2 < 0 || $value2 > 4){
                    $model->rollback();
                    $this->errorReturn("指标参数只能是1~4，分别是：1满意，2基本满意，3不满意，4，不了解");
                }
            }

            $new_data = array();
            $new_data['new_p_status'] = 1;
            $new_data['new_p_result'] = $examine_result;

            $update_result = M("TeamExamineRelation".$user_type)
                ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>$this->user_data['user_id']])
                ->save($new_data);
        }

        $model->commit();
        $this->successReturn("提交成功");
    }
}